{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from gs_quant.instrument import FXOption\n",
    "from gs_quant.risk import FXAnnualImpliedVol, Price, FXSpot\n",
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.common import MeasureScenario\n",
    "from gs_quant.markets import PricingContext\n",
    "import datetime as dt"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ce061973367468ad"
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-07-10T09:24:18.566753800Z",
     "start_time": "2025-07-10T09:24:12.780395500Z"
    }
   },
   "id": "8b13e419dcb97542"
  },
  {
   "cell_type": "markdown",
   "source": [
    "MeasureScenario class allows us to override inputs such as spot or volatility when pricing an instrument. Here are some examples of how to use it with FX Options."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2e9968f7e25e8115"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Initial FX Option\n",
    "fx_option = FXOption(pair='EURUSD', expiration_date='3m', premium=0)\n",
    "option = fx_option.calc((Price, FXAnnualImpliedVol, FXSpot))\n",
    "option.to_frame()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1a647b948e0ccc30"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Override a single input (Spot/Strike Price) with MeasureScenario context manager\n",
    "with MeasureScenario(FXSpot, 1.19):\n",
    "    fx_risks = fx_option.calc((Price, FXAnnualImpliedVol, FXSpot))\n",
    "fx_risks.to_frame()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "952616a7c77ff7c2"
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can see that the FXSpot value was overridden, and other variables are re-calculated to account for the new spot rate."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bec831f54e74bd45"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Override multiple inputs (Spot rate and Price) with MeasureScenario context manager\n",
    "with MeasureScenario(FXSpot, 1.19):\n",
    "    with MeasureScenario(FXAnnualImpliedVol, 0.05):\n",
    "        fx_risks = fx_option.calc((Price, FXAnnualImpliedVol, FXSpot))\n",
    "fx_risks.to_frame()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d5aebf58ca8fa787"
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can also combine the measure override with other context managers, such as PricingContext"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "85d3535611a8379"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Set up time context for pricing, and override the input\n",
    "with PricingContext(dt.date(2025, 7, 5)):\n",
    "    with MeasureScenario(FXSpot, 2):\n",
    "        fx_option_risks = fx_option.calc((Price, FXAnnualImpliedVol, FXSpot))\n",
    "fx_option_risks.result().to_frame()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3fdad1581e368a05"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "62d70efe106294bb"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
